package com.htfyun.factorytest.agingtest;

import android.content.Context;
import android.os.Message;
import android.os.PowerManager;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.NetworkUtils;
import com.htfyun.factorytest.RootApplication;

import com.htfyun.factorytest.R;

import com.htfyun.factorytest.common.Constants;
import com.htfyun.factorytest.utils.DebugLog;
import com.htfyun.factorytest.utils.TimeStringUtils;
import com.htfyun.factorytest.utils.UsefulUtils;

import java.io.File;

public class TestReboot extends AgingTestItemBase {

    private static final int MSG_COUNT_DOWN = 100;
    private static final int PERIOD_COUNT_DOWN = 5000;
    private static final int MSG_REBOOT = 200;

    private int countDown = 20;

    public TestReboot(@NonNull Context context, @NonNull View view) {
        super(context, view);
        if (getAgingTestItemAttr() != null
                && getAgingTestItemAttr().isTestOver(context)
                && AgingTestItemAttr.isRebootTestStarted(getContext())) {
            super.setTestStarted(true);
        }
    }

    @Override
    protected void handleMessage(Message msg) {
        final int what = msg.what;
        switch (what) {
            case MSG_COUNT_DOWN:
                updateUI();
                break;
            case MSG_REBOOT:
                reboot();
                break;
        }
    }

    @Override
    protected void doStartTest() {
        DebugLog.e("TestReboot doStartTest");
        AgingTestItemAttr.setAgingRebootTestEnabled(getContext(), true);
        //enable WiFi for test SDIO error
        NetworkUtils.setWifiEnabled(true);
        updateUI();
    }

    @Override
    protected void doStopTest() {
        DebugLog.e("TestReboot doStopTest");
        AgingTestItemAttr.setAgingRebootTestEnabled(getContext(), false);
        getHandler().removeMessages(MSG_COUNT_DOWN);
    }

    @Override
    public AgingTestItemAttr getAgingTestItemAttr() {
        return AgingTestItemAttr.reboot;
    }

    @Override
    protected void setTestStarted(boolean started) {
        super.setTestStarted(started);
        AgingTestItemAttr.setRebootTestStarted(getContext(), started);
    }

    private void updateUI() {

        countDown -= PERIOD_COUNT_DOWN / 1000;

        long usedTime = AgingTestItemAttr.getAgingTimeTick_ms(getContext()) - getAgingTestItemAttr().getStartTime_ms(getContext());

        String timeStr = TimeStringUtils.makeTimeString(usedTime);

        String info = getContext().getString(R.string.aging_reboot_info, countDown,
                getAgingTestItemAttr().getTestCount(getContext()),
                timeStr);

        String usbErrorCount = "";
        if (RootApplication.enableUsbCheck) {
            final int errorCount = AgingTestItemAttr.getSleepUSBErrorCount(getContext());
            usbErrorCount = getContext().getString(R.string.aging_usb_info, errorCount);
        }

        long startTimeOfReboot = getAgingTestItemAttr().getStartTimeAbsolute_ms(getContext());
        long now = System.currentTimeMillis();

        String timeInfo = getContext().getString(R.string.aging_reboot_time_info,
                UsefulUtils.millis2String(startTimeOfReboot),
                UsefulUtils.millis2String(now));

        updateTxtItemInfo(info + "\n" + timeInfo + "\n" + usbErrorCount);

        if (countDown <= 0) {
            if (RootApplication.enableUsbCheck && !new File(Constants.LTE_MODEL_PATH).exists()) {
                DebugLog.e("TestReboot usb error +1");
                int count = AgingTestItemAttr.getRebootUSBErrorCount(getContext());
                AgingTestItemAttr.setRebootUSBErrorCount(getContext(), ++count);
            }
            //Obtain wifi status to judge whether SDIO has an error
            if (!NetworkUtils.getWifiEnabled()) {
                DebugLog.e("SDIO status: fail +1");
                int count = AgingTestItemAttr.getWifiSdioErrorCount(getContext());
                AgingTestItemAttr.setWifiSdioErrorCount(getContext(), ++count);
            }
            getHandler().sendEmptyMessage(MSG_REBOOT);
        } else {
            getHandler().sendEmptyMessageDelayed(MSG_COUNT_DOWN, PERIOD_COUNT_DOWN);

        }
    }

    private void reboot() {

        int count = getAgingTestItemAttr().getTestCount(getContext());
        count++;
        getAgingTestItemAttr().setTestCount(getContext(), count);

        PowerManager pManager = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
        if (pManager != null) {
            pManager.reboot(null);
            DebugLog.e("reboot now");
        }
    }
}
